package com.opengroupe.dao.impl;

// Generated 31 juil. 2012 15:44:44 by Hibernate Tools 3.4.0.CR1

import java.util.List;

import javax.ejb.Stateless;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.opengroupe.dao.CandidatReponseQuestionDao;
import com.opengroupe.entities.Candidat;
import com.opengroupe.entities.CandidatReponseQuestion;
import com.opengroupe.entities.CandidatReponseQuestionId;
import com.opengroupe.entities.Question;

/**
 * Home object for domain model class Candidatreponsequestion.
 * @see com.opengroupe.entities.CandidatReponseQuestion
 * @author Hibernate Tools
 */
@Stateless
public class CandidatReponseQuestionDaoImpl extends JpaDaoSupport implements CandidatReponseQuestionDao{

	private static final Log log = LogFactory
			.getLog(CandidatReponseQuestionDaoImpl.class);

	public CandidatReponseQuestion persist(CandidatReponseQuestion transientInstance) throws DataAccessException{
		log.debug("persisting Candidatreponsequestion instance");
		try {
			getJpaTemplate().persist(transientInstance);
			log.debug("persist successful");
			return transientInstance;
		} catch (DataAccessException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void remove(CandidatReponseQuestion persistentInstance) {
		log.debug("removing Candidatreponsequestion instance");
		try {
			getJpaTemplate().remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public CandidatReponseQuestion merge(
			CandidatReponseQuestion detachedInstance) {
		log.debug("merging Candidatreponsequestion instance");
		try {
			CandidatReponseQuestion result = getJpaTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public CandidatReponseQuestion findById(CandidatReponseQuestionId id) {
		log.debug("getting Candidatreponsequestion instance with id: " + id);
		try {
			CandidatReponseQuestion instance = getJpaTemplate().find(
					CandidatReponseQuestion.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/**
	 * @deprecated 
	 * use {@link #findById(CandidatReponseQuestionId)} instead
	 */
	public CandidatReponseQuestion findById(Integer id) {
		return null;
	}

	@SuppressWarnings("unchecked")
	@Transactional
	@Override
	public List<CandidatReponseQuestion> deleteCRQs(Candidat candidat, Question question) {
		try {
			List<CandidatReponseQuestion> crqs = (List<CandidatReponseQuestion>)getJpaTemplate()
					.findByNamedQuery("findCRQs", candidat, question);
			for(CandidatReponseQuestion crq: crqs) {
				remove(crq);
			}
			return crqs;
		} catch(RuntimeException rte) {
			log.error("get CRQs failed", rte);
			return null;
		}
	}
}
